﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program22
{
    class Program
    {
        //1.用方法来实现：有一个字符串数组：{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。
        //2.用方法来实现：请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求：计算结果如果有小数，则显示小数点后两位（四舍五入）。
        //3.请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。
        static void Main(string[] args)
        {
            string[] strs = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
            Console.WriteLine(GetMaxLength(strs));

            int[] nums = { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 };
            Console.WriteLine(Math.Round( GetAverage(nums),2));

            int[] arr = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
            GetSort(arr);
            Console.WriteLine("从小到大：");
            foreach (var item in arr)
            {
                Console.Write(item+" ");
            }

            GetReverseSort(arr);
            Console.WriteLine("\r\n从大到小：");
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }

            Console.ReadLine();
        }

        /// <summary>
        /// 习题1，获取最长的字符串
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public static string GetMaxLength(string[] items)
        {
            int str = items[0].Length;
            string tmp = "";
            for (int i = 0; i < items.Length; i++)
            {
                if (str < items[i].Length)
                {
                    tmp = items[i];
                }
            }
            return tmp;
        }

        /// <summary>
        /// 习题2，求平均值
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static double GetAverage(int[] item)
        {
            int sum = 0;
            for (int i = 0; i < item.Length; i++)
            {
                sum += item[i];
            }
            return (double)sum/item.Length;
        }

        /// <summary>
        /// 习题3，正冒泡排序
        /// </summary>
        /// <param name="item"></param>
        public static void GetSort(int[] item)
        {
            for (int i = 1; i < item.Length; i++)
            {
                for (int j = 0; j < item.Length-i; j++)
                {
                    if (item[j] > item[j + 1])
                    {
                        int tmp = item[j];
                        item[j] = item[j + 1];
                        item[j + 1] = tmp;
                    }
                }
            }
        }

        /// <summary>
        /// 习题3，反冒泡排序
        /// </summary>
        /// <param name="item"></param>
        public static void GetReverseSort(int[] item)
        {
            for (int i = 1; i < item.Length; i++)
            {
                for (int j = 0; j < item.Length - i; j++)
                {
                    if (item[j] < item[j + 1])
                    {
                        int tmp = item[j];
                        item[j] = item[j + 1];
                        item[j + 1] = tmp;
                    }
                }
            }
        }
    }
}
